package com.planesnet.notification2;

import java.util.HashMap;
import java.util.Map;

import com.planesnet.notification2.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.EditText;

/**
 * Example of Notifications.
 * 
 * 
 * @see http
 *      ://developer.android.com/guide/topics/ui/notifiers/notifications.html
 *      Crea o actualiza una notificación en función del título. Visualiza una
 *      notificación de forma expandida.
 * @author "Luis Planes"
 * 
 */
public class MainActivity extends Activity {

	private EditText editTitle;
	private EditText editDescription;
	private CheckBox checkAutoCancel;

	private Notification notification;

	private Map<String, Notification> notifications = new HashMap<String, Notification>();
	private String title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		editTitle = (EditText) this.findViewById(R.id.editTitle);
		checkAutoCancel = (CheckBox) this.findViewById(R.id.checkAutoCancel);
		editDescription = (EditText) this.findViewById(R.id.editDescription);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * Crea o actualiza una notificación.
	 * 
	 * @param v
	 */
	public void onClickCreate(View v) {
		NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
				this).setSmallIcon(R.drawable.notification_icon);

		// Obtener el manager.
		NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		// Localizar si ya existe una notificación con el mismo título.
		title = editTitle.getText().toString();
		notification = notifications.get(title);

		// Si no existe la notificación, se crea una nueva.
		if (notification == null) {
			notification = new Notification();
			notification.setTitle(title);
			notification.setDescription(editDescription.getText().toString());
			notification.setAutoCancel(checkAutoCancel.isChecked());

			// Añadir la notificación a la lista.
			notifications.put(title, notification);

			// Modifica la información de la notificación.
		} else {

			notification.update();
			notification.setDescription(editDescription.getText().toString());
			notificationBuilder.setNumber(notification.getUpdates());

		}

		notificationBuilder.setContentTitle(notification.getTitle());
		notificationBuilder.setContentText(notification.getDescription());
		notificationBuilder.setAutoCancel(notification.isAutoCancel());

		// Creates an explicit intent for an Activity in your app
		Intent resultIntent = new Intent(this, ResultActivity.class);

		// Traspasar los datos de la notificación
		resultIntent.putExtra("title", notification.getTitle());
		resultIntent.putExtra("descriptions", notification.getDescriptions());

		// The stack builder object will contain an artificial back stack for
		// the started Activity.
		// This ensures that navigating backward from the Activity leads out of
		// your application to the Home screen.
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
		// Adds the back stack for the Intent (but not the Intent itself)
		stackBuilder.addParentStack(ResultActivity.class);
		// Adds the Intent that starts the Activity to the top of the stack
		stackBuilder.addNextIntent(resultIntent);
		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
				PendingIntent.FLAG_UPDATE_CURRENT);
		notificationBuilder.setContentIntent(resultPendingIntent);

		// id allows you to update the notification later on.
		notificationManager.notify(notification.getId(),
				notificationBuilder.build());

	}

	/**
	 * Borra una una notificación.
	 * 
	 * @param v
	 */
	public void onClickDelete(View v) {
		// Obtener el manager.
		NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		// Localizar si ya existe una notificación con el mismo título.
		notification = notifications.get(editTitle.getText().toString());

		// Si no existe la notificación, se crea una nueva.
		if (notification != null) {
			notificationManager.cancel(notification.getId());
			notifications.remove(notification);
		}

	}

	/**
	 * Borra todas las notificaciones.
	 * 
	 * @param v
	 */
	public void onClickDeleteAll(View v) {
		// Obtener el manager.
		NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		// Borrar todas las notificaciones.
		notificationManager.cancelAll();
		notifications.clear();
	}

}
